package com.wanczy.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wanczy.pojo.UserTable;
import com.wanczy.service.UserLoginService;
import com.wanczy.service.impl.UserLoginServiceImpl;


@WebServlet("/UserLoginServlet")
public class UserLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String userAccount = request.getParameter("userAccount");
		String userPass = request.getParameter("userPass");
		String path = request.getContextPath();
		
		UserLoginService userLoginService = new UserLoginServiceImpl();
		UserTable userTable = userLoginService.login(userAccount, userPass);
		
		if(null == userTable) {
			// ��֤ʧ�ܣ�ת���¼���棬������һ������error����ֵΪyes
			 out.print("<script>alert('用户名或密码错误');history.back();</script>");
			/*response.sendRedirect(path + "/jsp/login/login.jsp?error=yes");*/
		}else {
			HttpSession session = request.getSession();
//			session.setAttribute("id", userTable.getUser_id());//ѧ��EL���ʽ���Բ��������������
			session.setAttribute("userTable", userTable);
			response.sendRedirect(path + "/jsp/body.jsp");
		}		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

}
